package com.chenyun.message.tio;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import com.chenyun.message.MessageProperties;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.tio.cluster.TioClusterConfig;
import org.tio.cluster.redisson.RedissonTioClusterTopic;
import org.tio.http.common.HttpConfig;
import org.tio.server.ServerTioConfig;
import org.tio.utils.time.Time;
import org.tio.websocket.server.WsServerConfig;
import org.tio.websocket.server.WsServerStarter;

import java.io.Serializable;

/**
 * TioWebsocketStarter：
 * Description:
 *
 * @author: Lonni
 * @date: 2024/5/7 0007 15:04
 */
@Slf4j
public class TioWebsocketStarter implements Serializable {


    /**
     * 全局变量,其他地方使用
     */
    public static ServerTioConfig tioConfig;
    WsServerStarter serverStarter;


    MessageProperties properties;

    //一组连接共用的上下文对象
    // 在这里组装自定已的编解码器,如果不需要自定义,那么使用set方法手动设置
//    public static ServerTioConfig serverGroupContext = new ServerTioConfig(aioHandler, aioListener);
    public TioWebsocketStarter(MessageProperties properties) {
        this.properties = properties;
    }

    public void start() throws Exception {
        HttpConfig httpConfig = new HttpConfig(this.properties.getWsPort(),
                null, //默认1800
                null,//网站根路径
                this.properties.getWsPrefix());// 前缀 /ws
        httpConfig.setBindIp(this.properties.getWsIp());
        serverStarter = new WsServerStarter((WsServerConfig) httpConfig, new WsMsgHandler());

        //如果自定义 使用构造函数创建ServerTioConfig
        ServerTioConfig serverTioConfig = serverStarter.getServerTioConfig();
        serverTioConfig.setName("tio-websocket-proto-");
        serverTioConfig.setServerAioListener(ServerAioListenerImpl.me);
        serverTioConfig.setIpStatListener(ShowcaseIpStatListener.me);
        serverTioConfig.ipStats.addDurations(
                new Long[]{Time.MINUTE_1 * 5}
        );
        serverTioConfig.setHeartbeatTimeout(60);
        if (this.properties.getEnableCluster()) {
            RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
            Assert.notNull(redissonClient, "启动tio-ws失败,不存在redission相关依赖!");
            //创建集群的redissoon配置
            RedissonTioClusterTopic redissonTioClusterTopic =
                    new RedissonTioClusterTopic("bus-tio-topic", redissonClient);
            //集群配置文件,设置集群消息实现器
            TioClusterConfig tioClusterConfig = new TioClusterConfig(redissonTioClusterTopic);
            //开启集群模式
            tioClusterConfig.setCluster4group(true);
            serverTioConfig.setTioClusterConfig(tioClusterConfig);
        }

        if (this.properties.getEnableSsl()){
            serverTioConfig.useSsl(properties.getKeyStore(), properties.getTrustStore(), properties.getKeyStorePwd());
        }
        serverStarter.start();
        tioConfig = serverTioConfig;

    }

    public static ServerTioConfig getTioConfig() {
        return tioConfig;
    }

    public WsServerStarter getServerStarter() {
        return serverStarter;
    }


}



